package com.suliao.other.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author： M1nG
 * @since： 2020/9/7 12:15
 * @description： MybatisPlus代码生成器
 * @version： 1.0
 */
public class MybatisPlusGenerator {

    private static String TABLE_INCLUDE = "sys_";
    private static String TABLE_PREFIX = "sys_";

    private static String PROJECT_PATH = System.getProperty("user.dir");
    private static char FILE_SEPARATOR_CHAR = File.separatorChar;
    private static String RESOURCES_BASE_PATH = FILE_SEPARATOR_CHAR + "src" + FILE_SEPARATOR_CHAR + "main" + FILE_SEPARATOR_CHAR + "resources";
    private static String JAVA_BASE_PATH = FILE_SEPARATOR_CHAR + "src" + FILE_SEPARATOR_CHAR + "main" + FILE_SEPARATOR_CHAR + "java";

    private static String JDBC_URL = "jdbc:mysql://47.102.147.197:3306/wm_demo?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    private static String JDBC_USERNAME = "root";
    private static String JDBC_PASSWORD = "Suliao12345@";

    private static String PACKAGE_MODULE_NAME = "home";
    private static String PACKAGE_PARENT_NAME = "suliao";
    private static String PACKAGE_BASE_NAME = "com";


    public static void main(String[] args) {

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(PROJECT_PATH + FILE_SEPARATOR_CHAR + PACKAGE_PARENT_NAME + "-" + PACKAGE_MODULE_NAME + JAVA_BASE_PATH);
        gc.setActiveRecord(false);
        gc.setEnableCache(false);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setAuthor("M1nG-CodeGenerator");
        // gc.setDateType(DateType.ONLY_DATE);
        gc.setOpen(false);
        gc.setServiceName("%sService");
        //gc.setSwagger2(true); //实体属性 Swagger2 注解 api.jar  实体类加swagger 看情况
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(JDBC_URL);
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(JDBC_USERNAME);
        dsc.setPassword(JDBC_PASSWORD);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(PACKAGE_MODULE_NAME);
        pc.setParent(PACKAGE_BASE_NAME + "." + PACKAGE_PARENT_NAME);
        pc.setServiceImpl("serviceImpl");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("dataTime", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
                this.setMap(map);
            }
        };

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 如果模板引擎是 freemarker
        // 建议使用ftl 2020.09.09修改 M1nG
        String templateMapperXmlPath = "generator/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        // String templatePath = "/templates/mapper.xml.vm";
        focList.add(new FileOutConfig(templateMapperXmlPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String moduleFolder = PACKAGE_PARENT_NAME + "-" + PACKAGE_MODULE_NAME;
                return PROJECT_PATH + FILE_SEPARATOR_CHAR + moduleFolder + FILE_SEPARATOR_CHAR + moduleFolder + "-service" + RESOURCES_BASE_PATH + FILE_SEPARATOR_CHAR + "mapper" + FILE_SEPARATOR_CHAR +
                        tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });

        String templateEntityPath = "generator/templates/entity.java.ftl";
        focList.add(new FileOutConfig(templateEntityPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String moduleFolder = PACKAGE_PARENT_NAME + "-" + PACKAGE_MODULE_NAME;
                return PROJECT_PATH + FILE_SEPARATOR_CHAR + moduleFolder + FILE_SEPARATOR_CHAR + moduleFolder + "-api" + JAVA_BASE_PATH + FILE_SEPARATOR_CHAR +
                        PACKAGE_BASE_NAME + FILE_SEPARATOR_CHAR + PACKAGE_PARENT_NAME + FILE_SEPARATOR_CHAR + PACKAGE_MODULE_NAME + FILE_SEPARATOR_CHAR
                        + "entity" + FILE_SEPARATOR_CHAR + tableInfo.getEntityName() + "DO" + StringPool.DOT_JAVA;
            }
        });

        String templateControllerPath = "generator/templates/controller.java.ftl";
        focList.add(new FileOutConfig(templateControllerPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String moduleFolder = PACKAGE_PARENT_NAME + "-" + PACKAGE_MODULE_NAME;
                return PROJECT_PATH + FILE_SEPARATOR_CHAR + moduleFolder + FILE_SEPARATOR_CHAR + moduleFolder + "-service" + JAVA_BASE_PATH + FILE_SEPARATOR_CHAR +
                        PACKAGE_BASE_NAME + FILE_SEPARATOR_CHAR + PACKAGE_PARENT_NAME + FILE_SEPARATOR_CHAR + PACKAGE_MODULE_NAME + FILE_SEPARATOR_CHAR
                        + "controller" + FILE_SEPARATOR_CHAR + tableInfo.getEntityName() + "Controller" + StringPool.DOT_JAVA;
            }
        });

        String templateServicePath = "generator/templates/service.java.ftl";
        focList.add(new FileOutConfig(templateServicePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String moduleFolder = PACKAGE_PARENT_NAME + "-" + PACKAGE_MODULE_NAME;
                return PROJECT_PATH + FILE_SEPARATOR_CHAR + moduleFolder + FILE_SEPARATOR_CHAR + moduleFolder + "-api" + JAVA_BASE_PATH + FILE_SEPARATOR_CHAR +
                        PACKAGE_BASE_NAME + FILE_SEPARATOR_CHAR + PACKAGE_PARENT_NAME + FILE_SEPARATOR_CHAR + PACKAGE_MODULE_NAME + FILE_SEPARATOR_CHAR
                        + "service" + FILE_SEPARATOR_CHAR + tableInfo.getEntityName() + "Service" + StringPool.DOT_JAVA;
            }
        });

        String templateServiceImplPath = "generator/templates/serviceImpl.java.ftl";
        focList.add(new FileOutConfig(templateServiceImplPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String moduleFolder = PACKAGE_PARENT_NAME + "-" + PACKAGE_MODULE_NAME;
                return PROJECT_PATH + FILE_SEPARATOR_CHAR + moduleFolder + FILE_SEPARATOR_CHAR + moduleFolder + "-service" + JAVA_BASE_PATH + FILE_SEPARATOR_CHAR +
                        PACKAGE_BASE_NAME + FILE_SEPARATOR_CHAR + PACKAGE_PARENT_NAME + FILE_SEPARATOR_CHAR + PACKAGE_MODULE_NAME + FILE_SEPARATOR_CHAR
                        + "serviceImpl" + FILE_SEPARATOR_CHAR + tableInfo.getEntityName() + "ServiceImpl" + StringPool.DOT_JAVA;
            }
        });

        String templateMapperPath = "generator/templates/mapper.java.ftl";
        focList.add(new FileOutConfig(templateMapperPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String moduleFolder = PACKAGE_PARENT_NAME + "-" + PACKAGE_MODULE_NAME;
                return PROJECT_PATH + FILE_SEPARATOR_CHAR + moduleFolder + FILE_SEPARATOR_CHAR + moduleFolder + "-service" + JAVA_BASE_PATH + FILE_SEPARATOR_CHAR +
                        PACKAGE_BASE_NAME + FILE_SEPARATOR_CHAR + PACKAGE_PARENT_NAME + FILE_SEPARATOR_CHAR + PACKAGE_MODULE_NAME + FILE_SEPARATOR_CHAR
                        + "mapper" + FILE_SEPARATOR_CHAR + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_JAVA;
            }
        });



       /* cfg.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                // 判断自定义文件夹是否需要创建
                checkDir("调用默认方法创建的目录，自定义目录用");
                if (fileType == FileType.MAPPER) {
                    // 已经生成 mapper 文件判断存在，不想重新生成返回 false
                    return !new File(filePath).exists();
                }
                // 允许生成模板文件
                return true;
            }
        });*/
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        //配置自定义输出模板
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        templateConfig.setController("");
        templateConfig.setEntity("");
        templateConfig.setService("");
        templateConfig.setMapper("");
        templateConfig.setService("");
        templateConfig.setServiceImpl("");
        templateConfig.setXml("");
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass("com.suliao.common.base.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //公共父类
        //strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        //写于父类中的公共字段
        strategy.setSuperEntityColumns("createUser");
        strategy.setSuperEntityColumns("uploadUser");
        strategy.setSuperEntityColumns("createDate");
        strategy.setSuperEntityColumns("uploadDate");
        strategy.setEnableSqlFilter(false);
        strategy.setInclude("^" + TABLE_INCLUDE + "\\w*$");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(TABLE_PREFIX);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }


}
